package com.lanou3g.demo.servlet;

import com.lanou3g.demo.base.BaseServlet;

import com.lanou3g.demo.dao.BookDao;
import com.lanou3g.demo.dao.CategoryDao;
import com.lanou3g.demo.dao.OrderDao;
import com.lanou3g.demo.entity.BookEntity;
import com.lanou3g.demo.entity.CategoryEntity;
import com.lanou3g.demo.entity.OrderEntity;
import com.lanou3g.demo.entity.OrderItemEntity;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "AdminServlet",urlPatterns = "/admin/*")
public class AdminServlet extends BaseServlet {

    // 管理员登入页面显示
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/adminjsps/login.jsp").forward(request, response);
    }

    // 管理员页面显示上半部分
    public void top(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/top.jsp").forward(request, response);

    }

    // 管理员页面显示左半部分
    public void left(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/left.jsp").forward(request,response);

    }

    // 管理员页面显示body部分
    public void body(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/body.jsp").forward(request,response);

    }

    // 管理员登入页面
    public void loginDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminname = request.getParameter("adminname");
        String password = request.getParameter("password");
        if (adminname!=null && password!= null){
            request.getRequestDispatcher("/WEB-INF/adminjsps/admin/main.jsp").forward(request,response);
        }
    }

    // 全部订单显示页面
    public void orderListAll(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        List<OrderEntity> orderList = OrderDao.selectAllOrder();
        find(orderList);
        request.setAttribute("orderList",orderList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/order/list.jsp").forward(request,response);
    }

    //  未付款订单显示页面
    public void orderWFList(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        List<OrderEntity> orderList = OrderDao.selectWF();
        find(orderList);
        request.setAttribute("orderList",orderList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/order/list.jsp").forward(request,response);
    }

    // 已付款订单显示页面
    public void orderYFList(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        List<OrderEntity> orderList = OrderDao.selectYF();
        find(orderList);
        request.setAttribute("orderList",orderList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/order/list.jsp").forward(request,response);
    }

    // 未收货订单显示页面
    public void orderWSList(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        List<OrderEntity> orderList = OrderDao.selectWS();
        find(orderList);
        request.setAttribute("orderList",orderList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/order/list.jsp").forward(request,response);
    }

    // 已完成订单显示页面
    public void orderYSList(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        List<OrderEntity> orderList = OrderDao.selectYS();
        find(orderList);
        request.setAttribute("orderList",orderList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/order/list.jsp").forward(request,response);
    }

    // 订单页面工具方法
    public void find(List<OrderEntity> orderList) throws SQLException {
        for (OrderEntity order: orderList) {
            List<OrderItemEntity> orderItemList = OrderDao.selectOrderItem(order.getOid());
            order.setOrderItemList(orderItemList);
            for (OrderItemEntity orderItem : orderItemList) {
                BookEntity book = OrderDao.selectBook(orderItem.getBid());
                orderItem.setBookEntity(book);
            }
        }
    }



    //图书管理

    //查看所有图书
    public void bookList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<BookEntity> bookList = BookDao.BookAll();
        request.setAttribute("bookList",bookList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/book/list.jsp").forward(request,response);

    }

    //添加图书
    public void bookAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<CategoryEntity> categoryList = CategoryDao.categoryAll();
        request.setAttribute("categoryList",categoryList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/book/add.jsp").forward(request,response);

    }

    //跳转到图书的详情
    public void bookDesc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String bid = request.getParameter("bid");
        BookDao bookDao = new BookDao();
        BookEntity bookEntity = bookDao.bookEntity(bid);
        List<CategoryEntity> categoryList = CategoryDao.categoryAll();
        request.setAttribute("categoryList",categoryList);
        request.setAttribute("bookEntity",bookEntity);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/book/desc.jsp").forward(request,response);
    }



    // 郭玉亮种类方法

    public void categoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        // 在数据库中查询categoryList
        List<CategoryEntity> categoryEntityList = CategoryDao.categoryAll()
                ;
        request.setAttribute("categoryList",categoryEntityList);
        System.out.println(categoryEntityList);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/category/list.jsp").forward(request,response);
    }

    // 删除书的种类
    public void categoryDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        String cname = request.getParameter("cname");
        request.setAttribute("cid",cid);
        request.setAttribute("cname",cname);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/category/del.jsp").forward(request,response);
    }

    // 删除书的种类
    public void doCategoryDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String cid = req.getParameter("cid");
        CategoryDao.deleteCategory(cid);
        resp.sendRedirect(req.getContextPath()+"/admin/categoryList");

    }


    // 修改书的种类
    public void categoryMod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String cid = request.getParameter("cid");
        String cname = request.getParameter("cname");
        request.setAttribute("cid", cid);
        request.setAttribute("cname", cname);
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/category/mod.jsp").forward(request,response);
    }





    // 修改书的种类
    public void doCategoryMod(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        String cid = request.getParameter("cid");
        String cname = request.getParameter("cname");
        System.out.println(cname);
        CategoryDao.updateCategory(cid,cname);
        response.sendRedirect(request.getContextPath()+"/admin/categoryList");
    }

    // 添加书的种类
    public void categoryAdd (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        request.getRequestDispatcher("/WEB-INF/adminjsps/admin/category/add.jsp").forward(request,response);
    }

    // 添加书的种类
    public void doCategoryAdd (HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        String cid = request.getParameter("cid");
        String cname=request.getParameter("cname");
        CategoryDao.addCategory(cid,cname);
        response.sendRedirect(request.getContextPath()+"/admin/categoryList");
    }



}
